package cn.bdqn.controller;

import cn.bdqn.domain.SendFreight;
import cn.bdqn.service.SendFreightService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发货单控制层
 * @author dkj
 */
@RestController
public class SendFreightController {

    /**
     * 创建日志记录器
     */
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SendFreightService sendFreightService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/sendFreight/query/{id}")
    public Result queryByPrimaryKey(@PathVariable("id") Integer id){
        try {
            logger.info("进入了方法，参数：id={}",id);
            //根据id查询
            SendFreight sendFreight = sendFreightService.queryByPrimaryKey(id);
            //返回结果
            return ResultGenerator.genSuccessResult(sendFreight);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("报错了");
        }
    }


    /**
     * 根据发货单号模糊查询
     * @param sendFreightNumber
     * @return
     */
    @GetMapping("/sendFreight/querys/{sendFreightNumber}")
    public Result queryByLikeSendFreightNumber(@PathVariable("sendFreightNumber") String sendFreightNumber){

        try {
            logger.info("进入了方法,参数:sendFreightNumber={}",sendFreightNumber);
            //根据发货单号模糊查询
            List<SendFreight> sendFreights = sendFreightService.queryByLikeSendFreightNumber(sendFreightNumber);
            //返回结果
            return ResultGenerator.genSuccessResult(sendFreights);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 根据发货订单和支付类型和发货单状态查询
     * @param currentPage
     * @param pageSize
     * @param sendFreightNumber
     * @param paymentType
     * @param sendFreightState
     * @return
     */
    @GetMapping("/sendFreight/query/{currentPage}/{pageSize}/{sendFreightNumber}/{paymentType}/{sendFreightState}")
    public Result queryLikeSendFreightNumberByPaymentTypeBySendFreightState(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("pageSize") Integer pageSize,
            @PathVariable("sendFreightNumber") String sendFreightNumber,
            @PathVariable("paymentType") Integer paymentType,
            @PathVariable("sendFreightState") Integer sendFreightState){
        try {
            logger.info("进入了方法：参数：currentPage={},pageSize={},sendFreightNumber={},paymentType={},sendFreightState={}",currentPage,pageSize,sendFreightNumber,paymentType,sendFreightState);
            //多条件查询
            Page page = sendFreightService.queryLikeSendFreightNumberByPaymentTypeBySendFreightState(currentPage, pageSize, sendFreightNumber, paymentType, sendFreightState);
            //返回结果
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("报错了");

        }

    }

    /**
     * 新增
     * @param sendFreight
     */
    @PostMapping("/sendFreight/save/")
    public Result saveSendFreight(@RequestBody SendFreight sendFreight){
        try {

            logger.info("进入了方法：参数:sendFreight={}",sendFreight);
            //新增
            SendFreight sendFreight1 = sendFreightService.saveSendFreight(sendFreight);
            //返回
            return ResultGenerator.genSuccessResult(sendFreight1);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("报错了");
        }
    }

    /**
     * 更新
     * @param sendFreight
     */
    @PutMapping("/sendFreight/update/")
    public Result updateSendFreight(@RequestBody SendFreight sendFreight){
        try {
            logger.info("进入方法了，参数：sendFreight={}",sendFreight);
            //修改
            sendFreightService.updateSendFreight(sendFreight);
            //返回
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("报错了");
        }
    }
}
